home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / Little Smalltalk v3.1.4 / Smalltalk Source / mag.st < prev    next >
Encoding:
Text File  |  1994-06-11  |  17.2 KB  |  818 lines  |  [TEXT/KAHL]

  1. *
  2. * Little Smalltalk, version 3
  3. * Written by Tim Budd, Oregon State University, July 1988
  4. *
  5. * Classes dealing with objects having Magnitude
  6. *
  7. Class Magnitude Object
  8. Class    Date Magnitude    days months            "Added JRB, 6/94"
  9. Class    Char Magnitude value
  10. Class    Number Magnitude
  11. Class       Integer Number
  12. Class           LongInteger Integer negative digits
  13. Class       Fraction Number top bottom
  14. Class       Float Number
  15. Class Random Object
  16. *
  17. Methods Object 'magnitude'
  18.     isNumber
  19.         ^ false
  20. |
  21.     isFloat
  22.         ^ false
  23. |
  24.     isFraction
  25.         ^ false
  26. |
  27.     isInteger
  28.         ^ false
  29. |
  30.     isLongInteger
  31.         ^ false
  32. |
  33.     isShortInteger
  34.         ^ false
  35. ]
  36. Methods Date 'all'    "Methods added JRB, 6/94 - will be for date calx/conversions"
  37.     new
  38.         days <- Array new: 7.
  39.         days at: 1 put: 'Sunday'.
  40.         days at: 2 put: 'Monday'.
  41.         days at: 3 put: 'Tuesday'.
  42.         days at: 4 put: 'Wednesday'.
  43.         days at: 5 put: 'Thursday'.
  44.         days at: 6 put: 'Friday'.
  45.         days at: 7 put: 'Saturday'.
  46.  
  47.          months <- Array new: 12.
  48.         months at: 1  put: 'January'.
  49.         months at: 2  put: 'February'.
  50.         months at: 3  put: 'March'.
  51.         months at: 4  put: 'April'.
  52.         months at: 5  put: 'May'.
  53.         months at: 6  put: 'June'.
  54.         months at: 7  put: 'July'.
  55.         months at: 8  put: 'August'.
  56.         months at: 9  put: 'September'.
  57.         months at: 10 put: 'October'.
  58.         months at: 11 put: 'November'.
  59.         months at: 12 put: 'December'
  60. |
  61.     dayName: dayOfWeek
  62.         ^ days at: dayOfWeek
  63. |
  64.     monthName: monthNumber
  65.         ^ months at: monthNumber
  66. ]
  67. Methods Smalltalk 'current date & time in various formats'
  68.     currentDate        | s |
  69.         s <- <210 1>.
  70.         ^ (s copyFrom: 1 to: 11) , (s copyFrom: 21 to: 24)
  71. |
  72.     currentTime        | s |
  73.         s <- <210 1>.
  74.         ^ s copyFrom: 12 to: 19
  75. |
  76.     currentDayOfWeekNumber
  77.         ^ <210 2>
  78. |
  79.     currentDayName        | d |
  80.         d <- Date new.
  81.         ^ d dayName: (self currentDayOfWeekNumber)
  82. |
  83.     currentDayOfMonthNumber
  84.         ^ <210 3>
  85. |
  86.     currentMonthNumber
  87.         ^ <210 4>
  88. |
  89.     currentMonthName    | d |
  90.         d <- Date new.
  91.         ^ d monthName: (self currentMonthNumber)
  92. |
  93.     currentYear
  94.         ^ <210 5>
  95. ]
  96. Methods Char 'all'
  97.     < aValue
  98.         " can only compare characters to characters "
  99.         ^ aValue isChar
  100.             ifTrue: [ value < aValue asInteger ]
  101.             ifFalse: [ smalltalk error: 'char compared to nonchar']
  102. |
  103.     == aValue
  104.         ^ aValue isChar
  105.             ifTrue: [ value = aValue asInteger ]
  106.             ifFalse: [ false ]
  107. |
  108.     asInteger
  109.         ^ value
  110. |
  111.     asString
  112.         " make ourselves into a string "
  113.         ^ ' ' copy; at: 1 put: self
  114. |
  115.     digitValue
  116.         " return an integer representing our value "
  117.         self isDigit ifTrue: [ ^ value - $0 asInteger ].
  118.         self isUppercase ifTrue: [ ^ value - $A asInteger + 10 ].
  119.         ^ smalltalk error: 'illegal conversion, char to digit'
  120. |
  121.     isAlphabetic
  122.         ^ (self isLowercase) or: [ self isUppercase ]
  123. |
  124.     isAlphaNumeric
  125.         ^ (self isAlphabetic) or: [ self isDigit ]
  126. |
  127.     isBlank
  128.         ^ value = $   " blank char "
  129. |
  130.     isChar
  131.         ^ true
  132. |
  133.     isDigit
  134.         ^ value between: $0 asInteger and: $9 asInteger
  135. |
  136.     isLowercase
  137.         ^ value between: $a asInteger and: $z asInteger
  138. |
  139.     isUppercase
  140.         ^ value between: $A asInteger and: $Z asInteger
  141. |
  142.     value: aValue        " private - used for initialization "
  143.         value <- aValue
  144. |
  145.     printString
  146.         ^ '$', self asString
  147. ]
  148. Methods Fraction 'all'
  149.     = f
  150.         f isFraction
  151.             ifTrue: [ ^ (top = f top) and: [ bottom = f bottom ] ]
  152.             ifFalse: [ ^ super = f ]
  153. |
  154.     < f
  155.         f isFraction
  156.             ifTrue: [ ^ (top * f bottom) < (bottom * f top) ]
  157.             ifFalse:[ ^ super < f ]
  158. |
  159.     + f
  160.         f isFraction
  161.             ifTrue: [ ^ ((top * f bottom) + (bottom * f top)) /
  162.                     (bottom * f bottom) ]
  163.             ifFalse:[ ^ super + f ]
  164. |
  165.     - f
  166.         f isFraction
  167.             ifTrue: [ ^ ((top * f bottom) - (bottom * f top)) /
  168.                     (bottom * f bottom) ]
  169.             ifFalse:[ ^ super - f ]
  170. |
  171.     * f
  172.         f isFraction
  173.             ifTrue: [ ^ (top * f top) / (bottom * f bottom) ]
  174.             ifFalse: [ ^ super * f ]
  175. |
  176.     / f
  177.         ^ self * f reciprocal
  178. |
  179.     abs
  180.         ^ top abs / bottom
  181. |
  182.     asFloat
  183.         " convert to a floating point number "
  184.  
  185.         ^ top asFloat / bottom asFloat
  186. |
  187.     truncated
  188.         " convert to an integer rounded towards zero "
  189.         ^ top quo: bottom
  190. |
  191.     bottom
  192.         ^ bottom
  193. |
  194.     coerce: x
  195.         " coerce a value into being a fraction "
  196.  
  197.         ^ x asFraction
  198. |
  199.     generality
  200.         " generality value - used in mixed type arithmetic "
  201.         ^ 5
  202. |
  203.     isFraction
  204.         ^ true
  205. |
  206.     ln
  207.         ^ (top ln) - (bottom ln)
  208. |
  209.     raisedTo: x
  210.         ^ (top raisedTo: x) / (bottom raisedTo: x)
  211. |
  212.     reciprocal
  213.         ^ bottom / top
  214. |
  215.     top
  216.         ^ top
  217. |
  218.     with: t over: b
  219.         " initialization "
  220.  
  221.         top <- t.
  222.         bottom <- b
  223. |
  224.     printString
  225.         ^ top printString, '/', bottom printString
  226. ]
  227. Methods Float 'all'
  228.     + value
  229.         ^ value isFloat
  230.             ifTrue: [ <110 self value> " floating add " ]
  231.             ifFalse: [ super + value ]
  232. |
  233.     - value
  234.         ^ value isFloat
  235.             ifTrue: [ <111 self value> " floating subtract " ]
  236.             ifFalse: [ super - value ]
  237. |
  238.     < value
  239.         ^ value isFloat
  240.             ifTrue: [ <112 self value> " floating comparison " ]
  241.             ifFalse: [ super < value ]
  242. |
  243.     = value
  244.         ^ value isFloat
  245.             ifTrue: [ <116 self value> ]
  246.             ifFalse: [ super = value ]
  247. |
  248.     * value
  249.         ^ value isFloat
  250.             ifTrue: [ <118 self value> ]
  251.             ifFalse: [ super * value ]
  252. |
  253.     / value    
  254.         ^ value isFloat
  255.             ifTrue: [ (value = 0.0)
  256.                     ifTrue: [ smalltalk error:
  257.                         'float division by zero' ]
  258.                     ifFalse: [ <119 self value> ]]
  259.             ifFalse: [ super / value ]
  260. |
  261.     isFloat
  262.         ^ true
  263. |
  264.     coerce: value
  265.         " convert the value into a floating point number "
  266.         ^ value asFloat
  267. |
  268.     exp
  269.         " return e raised to self "
  270.         ^ <103 self>
  271. |
  272.     generality
  273.         " our numerical generality - used for mixed mode arithmetic"
  274.         ^ 7
  275. |
  276.     integerPart    | i j |
  277.         i <- <106 self>. j <- i basicAt: 2. i <- i basicAt: 1.
  278.         j < 0 ifTrue: [ ^ 0 ] ifFalse: [ ^ i * (2 raisedTo: j)]
  279. |
  280.     ln
  281.         " natural log of self "
  282.         ^ <102 self>
  283. |
  284.     new
  285.         ^ smalltalk error: 'cannot create floats with new'
  286. |
  287.     printString
  288.         ^ <101 self>
  289. |
  290.     quo: value
  291.         ^ (self / value) truncated
  292. |
  293.     rounded
  294.         ^ (self + 0.5) floor
  295. |
  296.     truncated    | result f i |
  297.         " truncate to an integer rounded towards zero"
  298.         f <- self. result <- 0.
  299.         [ i <- f integerPart. i > 0] whileTrue:
  300.             [ result <- result + i. f <- f - i ].
  301.         ^ result
  302. ]
  303. Methods Integer 'all'
  304.     + value        | r |
  305.         ^ (self isShortInteger and: [value isShortInteger])
  306.             ifTrue: [ r <- <60 self value>.
  307.                   "primitive will return nil on overflow"
  308.                   r notNil ifTrue: [ r ]
  309.                 ifFalse: [ self asLongInteger + value asLongInteger ]]
  310.             ifFalse: [ super + value ]
  311. |
  312.     - value        | r |
  313.         ^ (self isShortInteger and: [value isShortInteger])
  314.             ifTrue: [ r <- <61 self value>.
  315.                   "primitive will return nil on overflow"
  316.                 r notNil ifTrue: [ r ]
  317.                 ifFalse: [ self asLongInteger - value asLongInteger ]]
  318.             ifFalse: [ super - value ]
  319. |
  320.     < value
  321.         ^ (self isShortInteger and: [value isShortInteger])
  322.             ifTrue: [ <62 self value> ]
  323.             ifFalse: [ super < value ]
  324. |
  325.     > value
  326.         ^ (self isShortInteger and: [value isShortInteger])
  327.             ifTrue: [ <63 self value> ]
  328.             ifFalse: [ super > value ]
  329. |
  330.     = value
  331.         ^ (self isShortInteger and: [value isShortInteger])
  332.             ifTrue: [ self == value ]
  333.             ifFalse: [ super = value ]
  334. |
  335.     * value        | r |
  336.         ^ (self isShortInteger and: [value isShortInteger])
  337.             ifTrue: [ r <- <68 self value>.
  338.                   "primitive will return nil on overflow"
  339.                   r notNil ifTrue: [ r ]
  340.                   ifFalse: [ self asLongInteger * value asLongInteger ]]
  341.             ifFalse: [ super * value ]
  342. |
  343.     / value        | t b |
  344.         value = 0 ifTrue: [ ^ smalltalk error: 'division by zero'].
  345.  
  346.         value isInteger
  347.             ifTrue: [ b <- self gcd: value .
  348.                   t <- self quo: b.
  349.                   b <- value quo: b.
  350.                   b negative
  351.                     ifTrue: [ t <- t negated. 
  352.                           b <- b negated ].
  353.                   (b = 1) ifTrue: [ ^ t ].
  354.                   ^ Fraction new; with: t over: b ]
  355.             ifFalse: [ ^ super / value ]
  356. |
  357.     , value
  358.         " used to make long integer constants "
  359.         ^ self * 1000 + value
  360. |
  361.     allMask: value
  362.         " see if all bits in argument are on"
  363.         ^ value = (self bitAnd: value)
  364. |
  365.     anyMask: value
  366.         " see if any bits in argument are on"
  367.         ^ 0 ~= (self bitAnd: value)
  368. |
  369.     asCharacter
  370.         ^ Char new; value: self
  371. |
  372.     asDigit
  373.         " return as character digit "
  374.         (self >= 0)
  375.             ifTrue: [ (self <= 9) ifTrue: 
  376.                     [ ^ (self + $0 asInteger) asCharacter ].
  377.                   (self < 36) ifTrue:
  378.                     [ ^ (self + $A asInteger - 10) asCharacter ] ].
  379.         ^ smalltalk error: 'illegal conversion, integer to digit'
  380. |
  381.     asFloat
  382.         " should be redefined by any subclasses "
  383.         self isShortInteger ifTrue: [ ^ <51 self> ]
  384. |
  385.     asFraction
  386.         ^ Fraction new ; with: self over: 1
  387. |
  388.     asLongInteger    | newList i |
  389.         newList <- List new.
  390.         i = 0 ifTrue: [ newList add: 0 ]
  391.             ifFalse: [ i <- self abs.
  392.                    [ i ~= 0 ] whileTrue: 
  393.                     [ newList addLast: (i rem: 100).
  394.                     i <- i quo: 100 ] ].
  395.         ^ LongInteger new; sign: i negative digits: newList asArray
  396. |
  397.     asString
  398.         ^ self radix: 10
  399. |
  400.     bitAnd: value
  401.         ^ (self isShortInteger and: [value isShortInteger])
  402.             ifTrue: [ <71 self value > ]
  403.             ifFalse: [ smalltalk error: 
  404.                 'arguments to bit operation must be short integer']
  405. |
  406.     bitAt: value
  407.         ^ (self bitShift: 1 - value) bitAnd: 1
  408. |
  409.     bitInvert
  410.         "invert all bits in self"
  411.         ^ self bitXor: -1
  412. |
  413.     bitOr: value
  414.         ^ (self bitXor: value) bitXor: (self bitAnd: value)
  415. |
  416.     bitXor: value
  417.         ^ (self isShortInteger and: [value isShortInteger])
  418.             ifTrue: [ <72 self value > ]
  419.             ifFalse: [ smalltalk error: 
  420.                 'argument to bit operation must be integer']
  421. |
  422.     bitShift: value
  423.         ^ (self isShortInteger and: [value isShortInteger])
  424.             ifTrue: [ <79 self value > ]
  425.             ifFalse: [ smalltalk error: 
  426.                 'argument to bit operation must be integer']
  427. |
  428.     even
  429.         ^ (self rem: 2) = 0
  430. |
  431.     factorial
  432.         ^ (2 to: self) inject: 1 into: [:x :y | x * y ]
  433. |
  434.     gcd: value
  435.         (value = 0) ifTrue: [ ^ self ].
  436.         (self negative) ifTrue: [ ^ self negated gcd: value ].
  437.         (value negative) ifTrue: [ ^ self gcd: value negated ].
  438.         (value > self) ifTrue: [ ^ value gcd: self ].
  439.         ^ value gcd: (self rem: value)
  440. |
  441.     generality
  442.         " generality value - used in mixed class arithmetic "
  443.         ^ 2
  444. |
  445.     isShortInteger
  446.         ^ true
  447. |
  448.     lcm: value
  449.         ^ (self quo: (self gcd: value)) * value
  450. |
  451.     new
  452.         ^ smalltalk error: 'cannot create integers with new'
  453. |
  454.     odd
  455.         ^ (self rem: 2) ~= 0
  456. |
  457.     quo: value    | r |
  458.         ^ (self isShortInteger and: [value isShortInteger])
  459.             ifTrue: [ r <- <69 self value>.
  460.                 (r isNil)
  461.                     ifTrue: [ smalltalk error:
  462.                         'quo: or rem: with argument 0']
  463.                     ifFalse: [ r ]]
  464.             ifFalse: [ ^ super quo: value ]
  465. |
  466.     radix: base     | sa text |
  467.         " return a printed representation of self in given base"
  468.         sa <- self abs.
  469.         text <- (sa \\ base) asDigit asString.
  470.         ^ (sa < base)
  471.             ifTrue: [ (self negative)
  472.                     ifTrue: [ '-' , text ]
  473.                     ifFalse: [ text ]]
  474.             ifFalse: [ ((self quo: base) radix: base), text ]
  475. |
  476.     truncated
  477.         ^ self
  478. |
  479.     printString
  480.         ^ self asString
  481. |
  482.     timesRepeat: aBlock    | i |
  483.         " use while, which is optimized, not to:, which is not"
  484.         i <- 0.
  485.         [ i < self ] whileTrue:
  486.             [ aBlock value. i <- i + 1]
  487. ]
  488. Methods LongInteger 'all'
  489.     < n        | result |
  490.         n isLongInteger
  491.             ifFalse: [ ^ super < n ].
  492.         (negative == n negative) ifFalse: [ ^ negative ].
  493.         " now either both positive or both negative "
  494.         result <- false.
  495.         self with: n bitDo: 
  496.             [:x :y | (x ~= y) ifTrue: [ result <- x < y]].
  497.         negative ifTrue: [ result <- result not ].
  498.         ^ result
  499. |
  500.     = n
  501.         n isLongInteger
  502.             ifFalse: [ ^ super = n ].
  503.         (negative == n negative) ifFalse: [ ^ false ].
  504.         ^ digits = n digits
  505. |
  506.     + n        | newDigits z carry |
  507.         n isLongInteger
  508.             ifFalse: [ ^ super + n ].
  509.         negative ifTrue: [ ^ n - self negated ].
  510.         n negative ifTrue: [ ^ self - n negated ].
  511.         " reduced to positive + positive case "
  512.         newDigits <- List new.  carry <- 0.
  513.         self with: n bitDo:
  514.             [:x :y | z <- x + y + carry.
  515.                 (z >= 100) ifTrue: [ carry <- 1. z <- z - 100]
  516.                      ifFalse: [ carry <- 0 ].
  517.                 newDigits addLast: z ].
  518.         carry > 0 ifTrue: [ newDigits addLast: carry ].
  519.         ^ LongInteger new; sign: false digits: newDigits asArray
  520. |
  521.     - n        | result newDigits z borrow |
  522.         n isLongInteger
  523.             ifFalse: [ ^ super - n ].
  524.         negative ifTrue: [ ^ (self negated + n) negated ].
  525.         n negative ifTrue: [ ^ self + n negated ].
  526.         (self < n) ifTrue: [ ^ (n - self) negated ].
  527.         " reduced to positive - smaller positive "
  528.         newDigits <- List new. borrow <- 0.
  529.         self with: n bitDo:
  530.             [:x :y | z <- (x - borrow) - y.
  531.                 (z >= 0) ifTrue: [ borrow <- 0]
  532.                 ifFalse: [ z <- z + 100. borrow <- 1].
  533.                 newDigits addLast: z ].
  534.         result <- 0. "now normalize result by multiplication "
  535.         newDigits reverseDo: [:x | result <- result * 100 + x ].
  536.         ^ result
  537. |
  538.     * n        | result |
  539.         n isShortInteger ifTrue: [ ^ self timesShort: n ].
  540.         n isLongInteger  ifFalse: [ ^ super * n ].
  541.         result <- 0 asLongInteger.
  542.         digits reverseDo: 
  543.             [:x | result <- (result timesShort: 100) +
  544.                 (n timesShort: x)].
  545.         negative ifTrue: [ result <- result negated ].
  546.         ^ result
  547. |
  548.     abs
  549.         negative ifTrue: [ ^ self negated] 
  550. |
  551.     asFloat        | r |
  552.         r <- 0.0 .
  553.         digits reverseDo: [ :x | r <- r * 100.0 + x asFloat].
  554.         negative ifTrue: [ r <- r negated ].
  555.         ^ r.
  556. |
  557.     bitShift: n
  558.         (n >= 0)
  559.             ifTrue: [ ^ self * (2 raisedTo: n) ]
  560.             ifFalse: [ ^ self quo: (2 raisedTo: n negated)]
  561. |
  562.     coerce: n
  563.         ^ n asLongInteger
  564. |
  565.     digits
  566.         ^ digits
  567. |
  568.     generality
  569.         ^ 4 "generality value - used in mixed type arithmetic "
  570. |
  571.     isLongInteger
  572.         ^ true
  573. |
  574.     isShortInteger
  575.         " override method in class Integer "
  576.         ^ false
  577. |
  578.     negated
  579.         ^ LongInteger new; sign: negative not digits: digits
  580. |
  581.     negative
  582.         ^ negative
  583. |
  584.     new
  585.         "override restriction from class Integer"
  586.         ^ self
  587. |
  588.     quo: value    | a b quo result |
  589.         result <- 0.
  590.         a <- self abs. b <- value abs.
  591.         [a > b] whileTrue:
  592.             [ quo <- (a asFloat quo: b). result <- result + quo.
  593.                 a <- a - (b * quo) ].
  594.         ^ result
  595. |
  596.     sign: s digits: d
  597.         negative <- s.
  598.         digits <- d.
  599. |
  600.     printString    | str |
  601.         str <- negative ifTrue: [ '-' ] ifFalse: [ '' ].
  602.         digits reverseDo: [:x | str <- str , 
  603.             (x quo: 10) printString , (x rem: 10) printString ].
  604.         ^ str
  605. |
  606.     timesShort: value    | y z carry newDigits |
  607.         y <- value abs.
  608.         carry <- 0.
  609.         newDigits <- digits collect:
  610.             [:x | z <- x * y + carry. 
  611.                 carry <- z quo: 100. 
  612.                 z - (carry * 100)].
  613.         (carry > 0) ifTrue: [ newDigits <- newDigits grow: carry ].
  614.         ^ LongInteger new; sign: (negative xor: value negative) 
  615.                     digits: newDigits
  616. |
  617.     with: n bitDo: aBlock    | d di dj |
  618.         " run down two digits lists in parallel doing block "
  619.         di <- digits size.
  620.         d <- n digits.
  621.         dj <- d size.
  622.         (1 to: (di max: dj)) do: [:i |
  623.             aBlock value: 
  624.                ((i <= di) ifTrue: [ digits at: i] ifFalse: [0])
  625.                 value:
  626.                ((i <= dj) ifTrue: [ d at: i] ifFalse: [0]) ]
  627. ]
  628. Methods Magnitude 'all'
  629.     <= value
  630.         ^ (self < value) or: [ self = value ]
  631. |
  632.     < value
  633.         ^ (self <= value) and: [ self ~= value ]
  634. |
  635.     >= value
  636.         ^ value <= self
  637. |
  638.     > value
  639.         ^ (value < self)
  640. |
  641.     = value
  642.         ^ (self == value)
  643. |
  644.     ~= value
  645.         ^ (self = value) not
  646. |
  647.     between: low and: high
  648.         ^ (low <= self) and: [ self <= high ]
  649. |
  650.     isChar
  651.         ^ false
  652. |
  653.     max: value
  654.         ^ (self < value)
  655.             ifTrue: [ value ]
  656.             ifFalse: [ self ]
  657. |
  658.     min: value
  659.         ^ (self < value)
  660.             ifTrue: [ self ]
  661.             ifFalse: [ value ]
  662. ]
  663. Methods Number 'all'
  664.     isNumber
  665.         ^ true
  666. |
  667.     maxgen: value
  668.         (self isNumber and: [ value isNumber ])
  669.             ifFalse: [ ^ smalltalk error: 
  670.                 'arithmetic on non-numbers' ].
  671.         ^ (self generality > value generality)
  672.             ifTrue: [ self ]
  673.             ifFalse: [ value coerce: self ]
  674. |
  675.     + value
  676.         ^ (self maxgen: value) + (value maxgen: self)
  677. |
  678.     - value
  679.         ^ (self maxgen: value) - (value maxgen: self)
  680. |
  681.     < value
  682.         ^ (self maxgen: value) < (value maxgen: self)
  683. |
  684.     = value
  685.         ^ value isNumber
  686.             ifTrue: [ (self maxgen: value) = (value maxgen: self) ]
  687.             ifFalse: [ false ]
  688. |
  689.     * value
  690.         ^ (self maxgen: value) * (value maxgen: self)
  691. |
  692.     / value
  693.         ^ (self maxgen: value) / (value maxgen: self)
  694. |
  695.     // value
  696.         " integer division, truncate towards negative infinity"
  697.         " see quo: "
  698.         ^ (self / value) floor
  699. |
  700.     \\ value
  701.         " remainder after integer division "
  702.         ^ self - (self // value * value)
  703. |
  704.     abs
  705.         ^ (self < 0)
  706.             ifTrue: [ 0 - self ]
  707.             ifFalse: [ self ]
  708. |
  709.     ceiling        | i |
  710.         i <- self truncated.
  711.         ^ ((self positive) and: [ self ~= i ])
  712.             ifTrue: [ i + 1 ]
  713.             ifFalse: [ i ]
  714. |
  715.     copy
  716.         ^ self
  717. |
  718.     exp
  719.         ^ self asFloat exp
  720. |
  721.     floor        | i |
  722.         i <- self truncated.
  723.         ^ ((self negative) and: [ self ~= i ])
  724.             ifTrue: [ i - 1 ]
  725.             ifFalse: [ i ]
  726. |
  727.     fractionalPart
  728.         ^ self - self truncated
  729. |
  730.     isInteger
  731.         ^ self isLongInteger or: [ self isShortInteger ]
  732. |
  733.     ln
  734.         ^ self asFloat ln
  735. |
  736.     log: value
  737.         ^ self ln / value ln
  738. |
  739.     negated
  740.         ^ 0 - self
  741. |
  742.     negative
  743.         ^ self < 0
  744. |
  745.     positive
  746.         ^ self >= 0
  747. |
  748.     quo: value
  749.         ^ (self maxgen: value) quo: (value maxgen: self)
  750. |
  751.     raisedTo: x    | y |
  752.         x negative 
  753.             ifTrue: [ ^ 1 / (self raisedTo: x negated) ].
  754.         x isShortInteger 
  755.             ifTrue: [ (x = 0) ifTrue: [ ^ 1 ].
  756.                   y <- (self raisedTo: (x quo: 2)) squared.
  757.                   x odd ifTrue: [ y <- y * self ].
  758.                   ^ y ]
  759.                 "use logrithms to do exponeneation"
  760.             ifFalse: [ ^ ( x * self ln ) exp ]
  761. |
  762.     reciprocal
  763.         ^ 1 / self
  764. |
  765.     rem: value
  766.         ^ self - ((self quo: value) * value)
  767. |
  768.     roundTo: value
  769.         ^ (self / value ) rounded * value
  770. |
  771.     sign
  772.         ^ (self = 0) ifTrue: [ 0 ]
  773.             ifFalse: [ self / self abs ]
  774. |
  775.     sqrt
  776.         ^ (self negative)
  777.             ifTrue: [ smalltalk error: 'sqrt of negative']
  778.             ifFalse: [ self raisedTo: 0.5 ]
  779. |
  780.     squared
  781.         ^ self * self
  782. |
  783.     strictlyPositive
  784.         ^ self > 0
  785. |
  786.     to: value
  787.         ^ Interval new; lower: self; upper: value; step: 1
  788. |
  789.     to: value by: step
  790.         ^ Interval new; lower: self; upper: value; step: step
  791. |
  792.     trucateTo: value
  793.         ^ (self / value) trucated * value
  794. ]
  795. Methods Random 'all'
  796.     between: low and: high
  797.         " return random number in given range "
  798.         ^ (self next * (high - low)) + low
  799. |
  800.     next
  801.         " convert rand integer into float between 0 and 1 "
  802.         ^ (<3> rem: 1000) / 1000
  803. |
  804.     next: value    | list |
  805.         " return a list of random numbers of given size "
  806.         list <- List new.
  807.         value timesRepeat: [ list add: self next ].
  808.         ^ list
  809. |
  810.     randInteger: value
  811.         ^ 1 + (<3> rem: value)
  812. |
  813.     set: value
  814.         " set seed for random number generator "
  815.         <55 value>
  816. ]
  817.  
  818.